﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Admin.Models.ConfigManager;
using Zocono.WCS.Admin.Models.User;
using Zocono.WCS.Application.AppConfigureManage;
using Zocono.WCS.Application.AppTraces;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;

namespace Zocono.WCS.Admin.Controllers.ConfigManageMoudle
{
    public class SCRowInfoController : BaseController
    {
        private readonly ScRowInfoApp _scRowInfoApp;

        public SCRowInfoController(ScRowInfoApp scRowInfoApp, IMemoryCache memoryCache, TracesApp tracesApp, IHttpContextAccessor httpContextAccessor) : base(memoryCache, tracesApp, httpContextAccessor)
        {
            this._scRowInfoApp = scRowInfoApp;
        }

        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 堆垛机排号配置页面
        /// </summary>
        /// <returns></returns>
        public ActionResult SCRowInfoAsync()
        {
            //await AddTracesInfo();
            return View();
        }

        /// <summary>
        /// 获取堆垛机排号数据
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> GetAllSCRowInfoAsync()
        {
            var where = new QueryScRowInfoDto();
            where.PageIndex = (Start / Length) + 1;
            where.PageSize = Length;
            where.ScEquipmentCode = Request.Form["ScEquipmentCode"].ToString();
            where.Row = Request.Form["Row"].ToString();

            var scRowInfos = await _scRowInfoApp.GetPagedScRowInfoAsync(where);

            var scRowPageData = new List<ScRowInfoPageData>();
            if (scRowInfos != null && scRowInfos.Result != null && scRowInfos.Result.Count > 0)
            {
                foreach (var item in scRowInfos.Result)
                {
                    scRowPageData.Add(new ScRowInfoPageData()
                    {
                        ID = item.ID,
                        Row = item.Row,
                        ScEquipmentCode = item.ScEquipmentCode
                    });
                }
            }

            var pageData = new TableData<ScRowInfoPageData>(Draw, scRowInfos.Total, scRowInfos.Total, scRowPageData);

            //await AddTracesInfo(5, 0, "获取堆垛机排号配置数据");
            return Json(pageData);
        }
        /// <summary>
        /// 添加或者编辑页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ActionResult> ScRowInfoAddOrEditAsync(long id)
        {
            var scRowPageData = new ScRowInfoPageData();
            if (id > 0)
            {
                var scRowInfo = await _scRowInfoApp.GetScRowInfoByIdAsync(id);
                if (scRowInfo != null)
                {
                    scRowPageData.ID = scRowInfo.ID;
                    scRowPageData.Row = scRowInfo.Row;
                    scRowPageData.ScEquipmentCode = scRowInfo.ScEquipmentCode;
                }
            }

            return View(scRowPageData);
        }
        /// <summary>
        /// 操作
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> OperateScRowInfoAsync()
        {
            var id = Request.Form["id"].ToInt64();
            var scRowInfo = new ScRowInfo();
            scRowInfo.ID = id;
            scRowInfo.ScEquipmentCode = Request.Form["ScEquipmentCode"].ToString();
            scRowInfo.Row = Request.Form["Row"].ToString();

            bool result;
            string msg;
            try
            {
                if (scRowInfo.ID == 0)
                {
                    result = await _scRowInfoApp.CreateScRowInfoAsync(scRowInfo);
                    await AddTracesInfo(2, 0, "新增堆垛机货排配置");
                }
                else
                {
                    result = await _scRowInfoApp.ModifyScRowInfoAsync(scRowInfo);
                    await AddTracesInfo(3, 0, "修改堆垛机货排配置");
                }
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }
            return  CreateJsonGetResult(result, msg);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> DeleteScRowInfoAsync()
        {
            var id = Request.Form["id"].ToInt64();
            bool result;
            string msg;
            try
            {
                result = await _scRowInfoApp.DeleteScRowInfoByIdAsync(id);
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }
            await AddTracesInfo(4, 0, "删除堆垛机货排配置");
            return CreateJsonGetResult(result, msg);
        }

    }
}
